//
//  CLInfoSelectVC.m
//  togetherLBS
//
//  Created by Phyllis Hu on 15-5-25.
//  Copyright (c) 2015年 celi. All rights reserved.
//

#import "CLInfoSelectVC.h"

@interface CLInfoSelectVC ()
@property (nonatomic, strong) NSArray *data;
@property (nonatomic, strong) NSMutableArray *selection;
@property (nonatomic, strong) NSArray *infos;
@property (nonatomic, copy) NSString* fid;
@end

@implementation CLInfoSelectVC

- (instancetype)initWithData:(NSArray *)data selectionData:(NSMutableArray *)selectionData
{
    if (self = [super initWithStyle:UITableViewStyleGrouped]) {
        self.selection = [NSMutableArray arrayWithArray:selectionData];
        
        NSMutableArray *infoArray = [NSMutableArray array];
        for (NSDictionary *dict in data) {
            CLInfo *info = [[CLInfo alloc] initWithDict:dict];
            [infoArray addObject:info];
        }
        self.infos = infoArray;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return self.infos.count - 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"infoSelect";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewStyleGrouped reuseIdentifier:cellID];
    }
    
    cell.textLabel.text = [self.infos[indexPath.row + 1] name];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CLInfo *info = self.infos[indexPath.row + 1];
    [self.selection removeObject:info.name];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CLInfo *info = self.infos[indexPath.row + 1];
    
    // 判断当前选择是否为第一次
    if (self.selection.count > self.index) {
        [self.selection removeLastObject];
    }
    [self.selection addObject:info.name];

    // 判断是否要跳转到下一级界面 sub有值 selection数组已经有2个值
    if (info.sub != nil && self.selection.count < 3) {
        // 跳转到下一个界面
        CLInfoSelectVC *selectVC = [[CLInfoSelectVC alloc] initWithData:info.sub selectionData:self.selection];
        selectVC.member = self.member;
        selectVC.block = self.block;
        selectVC.index = self.index + 1;
        [self.navigationController pushViewController:selectVC animated:YES];
    } else {
        // 发送网络请求
        DLog(@"发送网络请求  设置地区")
        // 更改数据成功后
        self.block(self.selection);
        
        NSInteger vcIndex = self.navigationController.viewControllers.count - self.selection.count - 1;
        UIViewController *vc = [self.navigationController.viewControllers objectAtIndex:vcIndex];
        [self.navigationController popToViewController:vc animated:YES];
        
            /*CLMemberSettingParam *param = [CLMemberSettingParam param];
            param.fid = self.fid;
            NSMutableDictionary *dict = [NSMutableDictionary dictionary];
            dict[@"country"] = self.selection[0];
            if (self.selection.count > 1) {
                dict[@"province"] = self.selection[1];
            }
            if (self.selection.count > 2) {
                dict[@"city"] = self.selection[2];
            }
            param.district = dict.JSONString;
            
            [CLMemberTool setMemberInfoWithParam:param success:^(CLBaseResult *result) {
                if (result.r.integerValue == 1) {
   
                    // 更改数据成功后
                    self.block(self.selection);
                    
                    NSInteger vcIndex = self.navigationController.viewControllers.count - self.selection.count - 1;
                    UIViewController *vc = [self.navigationController.viewControllers objectAtIndex:vcIndex];
                    [self.navigationController popToViewController:vc animated:YES];
                } else {
                    [CLService showAlertWithMessage:result.errorMessage];
                }
            } failure:^(NSError *error) {
                
            }];*/
    }
}

@end

@implementation CLInfo
+ (instancetype)infoWithDict:(NSDictionary *)dict
{
    return [[self alloc] initWithDict:dict];
}
- (instancetype)initWithDict:(NSDictionary *)dict
{
    if (self = [super init]) {
        [self setValuesForKeysWithDictionary:dict];
    }
    return self;
}
@end
